package com.Service.impl;

import cn.hutool.core.util.NumberUtil;
import com.Bean.Good;
import com.Bean.Provider;
import com.Bean.User;
import com.Common.CodeMsg;
import com.Common.PageInfo;
import com.Common.Result;
import com.Dao.GoodDao;
import com.Service.GoodService;
import org.junit.Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: hkw14
 * @time: 2021/5/2 22:07
 */
public class GoodServiceImpl implements GoodService {
    GoodDao goodDao = new GoodDao();

    @Override
    public Result deleteGood(String... ids) {
        boolean flag = goodDao.batchDelete(ids);
        return flag?new Result():new Result(CodeMsg.GOOD_DELETE_ERROR);
    }
    @Override
    public Result addGood(Integer ID, Integer number) {
        return null;
    }

    @Override
    public Result sealGood(Integer ID, Integer number) {
        return null;
    }

    @Override
    public Result newGoods( String goodName, String provider, String description,String goodsType,String brand) {
        boolean b = goodDao.insertGood(goodName, provider, goodsType, brand, description);
        if (b){
            return new Result();
        }else {
            return new Result(CodeMsg.GOOD_ADD_ERROR);
        }
    }

    @Override
    public Result queryPage(Map<String, Object> params, String page, String limit) {
        Integer pageNo =  NumberUtil.isNumber(page)?Integer.parseInt(page):1;
        Integer pageSize =  NumberUtil.isNumber(limit)?Integer.parseInt(limit):10;
        PageInfo<Good> pageInfo = goodDao.selectPage(params, pageNo, pageSize);
        return new Result(pageInfo);
    }

    @Override
    public Result getGoodList() {
        List<Good> providerList = goodDao.getGoodList();
        Result result = new Result(providerList);
        return result;
    }

    @Override
    public Result getGoodbyID(Integer ID) {
        Good good = goodDao.getGoodByID(ID);
        Result result = new Result(good);
        return result;
    }

    @Test
    public void test(){
        GoodService goodService = new GoodServiceImpl();
        Map<String,Object> params = new HashMap<String,Object>();
        Result result = goodService.queryPage(params, null, null);
        PageInfo data = (PageInfo)result.getData();
        List list = data.getList();
        System.out.println(data.getCount());
        Iterator<Good> iterator = list.iterator();

        while (iterator.hasNext()){
            Good next = iterator.next();
            System.out.println(next);
        }
    }

    @Test
    public void test2(){
        GoodService goodService = new GoodServiceImpl();
        Result goodbyID = goodService.getGoodbyID(2);
        Good data = (Good) goodbyID.getData();
        System.out.println(data);
    }
}
